<template>
	<view class="purchaseTransOrder">
		<uni-nav-bar color="#ffffff" background-color="#00aded" :statusBar="true" title="生产运单" fixed="true" left-icon="left" left-text="返回" @clickLeft="back"></uni-nav-bar>
		<view class="purchaseTransOrder_top">
			<!-- 搜索 -->
			<dil-search :columnData="options.columnDataList" @onSearch="onSearch" v-if="options.columnDataList.length > 0"></dil-search>
		</view>
		<view class="purchaseTransOrder_main">
			<view class="dilSeg">
				<uni-segmented-control :current="current" :values="items" :style-type="styleType" :active-color="activeColor" @clickItem="onClickItem" />
			</view>
			<dil-card class="dilCard" @emitEvent="emitEvent" v-bind.sync="options"></dil-card>
		</view>
		<view class="purchaseTransOrder_floot"></view>
	</view>
</template>

<script>
import { mapMutations } from 'vuex';
export default {
	data() {
		return {
			params: {},
			items: ['未完成', '已完成'],
			styles: [
				{
					value: 'button',
					text: '按钮',
					checked: true
				},
				{
					value: 'text',
					text: '文字'
				}
			],
			colors: ['#007aff', '#4cd964', '#dd524d'],
			current: 0,
			colorIndex: 0,
			activeColor: '#007aff',
			styleType: 'button',
			options: {
				requestUrl: '/api/v1/tms/getTransOrderList?apiId=665',
				requestQuery: {},
				cardSlotList: [
					{
						label: '作业详情',
						icon: 'eye',
						emitEvent: 'transOrderDetails',
						noPre: '1'
					},
					{
						label: '启用',
						icon: 'checkmarkempty',
						emitEvent: 'notBan',
						noPre: '1'
					},
					{
						label: '禁用',
						icon: 'closeempty',
						emitEvent: 'ban',
						noPre: '1'
					},
					{
						label: '删除',
						icon: 'trash',
						emitEvent: 'transOrderDel',
						noPre: '1'
					}
				],
				columnDataList: [],
				totalNum: 0
			}
		};
	},
	onLoad(options) {
		if (options.params) {
			this.params = JSON.parse(options.params);
		}
	},
	onShow() {
		uni.$emit('onShow', 1);
		this.getOrderSection();
	},
	mounted() {
		this.getOrderSectionDebounce = this.Tool.debounce((data) => {
			this.getOrderSection(data);
		}, 500);
	},
	methods: {
		onClickItem(item) {
			this.current = item.currentIndex;
			this.getOrderSectionDebounce();
		},
		back() {
			let page = getCurrentPages();
			if (page.length > 1) {
				let pageUrl = page[page.length - 2].route;
				console.log(pageUrl, 'pageUrl');
				uni.reLaunch({
					url: '/' + pageUrl
				});
			} else {
				uni.switchTab({
					url: '/pages/index/index'
				});
			}
		},
		async getOrderSection(data = {}) {
			if (this.current == 0) {
				data.statusList = ['未确认', '已确认', '未知状态'];
			} else {
				data.statusList = ['已完成'];
			}
			data.businessScene = '生产';
			data = { ...data, ...this.params };
			this.options.requestQuery = { ...data };
		},
		onSearch(item) {
			this.getOrderSectionDebounce(item);
		},
		emitEvent(eventName, cardItem) {
			this[eventName](cardItem);
		},
		//作业详情
		transOrderDetails(cardItem) {
			cardItem.apiId = 665;
			this.updateTransOrderItem(cardItem);
			uni.navigateTo({
				url: '/pages/transOrder/transOrderDetail'
			});
		},
		async changeTransOrder(data) {
			const res = await this.$myRequest({
				url: '/api/v1/tms/changeTransOrder',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'none'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					duration: 2000,
					title: '删除成功',
					icon: 'success'
				});
				this.getOrderSectionDebounce();
			} else {
				uni.showToast({
					duration: 2000,
					title: res.data.message,
					icon: 'none'
				});
			}
		},
		transOrderDel(cardItem) {
			let that = this;
			uni.showModal({
				cancelText: '取消',
				confirmText: '确认',
				title: '请填写删除原因',
				content: '',
				placeholderText: '请填写删除原因',
				editable: true,
				success: (res) => {
					if (res.confirm) {
						if (!res.content || res.content == 0) {
							uni.showToast({
								title: '删除失败！请填写删除原因！',
								icon: 'none',
								duration: 3000
							});
						} else {
							cardItem.deleteReason = res.content;
							cardItem.deleted = 1;
							that.changeTransOrder(cardItem);
						}
					} else if (res.cancel) {
						uni.showToast({
							title: '用户取消操作！',
							icon: 'none',
							duration: 3000
						});
					}
				}
			});
		},
		transOrderUpd(cardItem) {
			let that = this;
			uni.showModal({
				cancelText: '取消',
				confirmText: '确认',
				title: '请填写原发数量',
				content: '',
				placeholderText: '请填写原发数量',
				editable: true,
				success: (res) => {
					if (res.confirm) {
						try {
							res.content = Number.parseFloat(res.content);
						} catch (e) {}
						if (typeof res.content != 'number') {
							uni.showToast({
								title: '修改失败！请填写数字！',
								icon: 'none',
								duration: 3000
							});
						} else if (res.content <= 0) {
							uni.showToast({
								title: '修改失败！原发数量必须大于0！',
								icon: 'none',
								duration: 3000
							});
						} else {
							cardItem.materialNumber = res.content;
							that.updateTransOrder(cardItem);
						}
					} else if (res.cancel) {
						uni.showToast({
							title: '用户取消操作！',
							icon: 'none',
							duration: 3000
						});
					}
				}
			});
		},
		async updateTransOrder(data) {
			const res = await this.$myRequest({
				url: '/api/v1/tms/updateTransOrder',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'none'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					duration: 2000,
					title: '修改成功',
					icon: 'success'
				});
				this.getOrderSectionDebounce();
			} else {
				uni.showToast({
					duration: 2000,
					title: res.data.message,
					icon: 'none'
				});
			}
		},
		notBan(cardItem){
			let that = this;
			uni.showModal({
				cancelText: '取消',
				confirmText: '确认',
				title: '确认启用？',
				success: (res) => {
					if (res.confirm) {
						cardItem.isBan = 0;
						that.banTransOrder(cardItem);
					} else if (res.cancel) {
						uni.showToast({
							title: '用户取消操作！',
							icon: 'none',
							duration: 3000
						});
					}
				}
			});
		},
		ban(cardItem){
			let that = this;
			uni.showModal({
				cancelText: '取消',
				confirmText: '确认',
				title: '确认禁用？',
				success: (res) => {
					if (res.confirm) {
						cardItem.isBan = 1;
						that.banTransOrder(cardItem);
					} else if (res.cancel) {
						uni.showToast({
							title: '用户取消操作！',
							icon: 'none',
							duration: 3000
						});
					}
				}
			});
		},
		async banTransOrder(data){
			const res = await this.$myRequest({
				url: '/api/v1/tms/banTransOrder',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'none'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					duration: 2000,
					title: '操作成功',
					icon: 'success'
				});
				this.getOrderSectionDebounce();
			} else {
				uni.showToast({
					duration: 2000,
					title: res.data.message,
					icon: 'none'
				});
			}
		},
		//vuex模块
		...mapMutations('common', ['updateTransOrderItem'])
	}
};
</script>

<style lang="scss" scoped>
.purchaseTransOrder {
	width: 100vw;
	height: 100%;
	background-color: #f5f5f5;
	&_top {
		background-color: #fff;
	}
	&_main {
		margin-top: 20rpx;
		background-color: #fff;
		.dilSeg {
			width: 96vw;
			margin: auto;
		}
	}
	&_floot {
		background-color: #fff;
	}
}
</style>
